import wepy from 'wepy';
import stringUtil from './stringUtil';
import {LOGIN} from "../api/api";


export default class httpUtil {

  static GET = 'GET';
  static POST = 'POST';

  /**
   * 网络请求
   * @param url
   * @param params
   * @param method
   * @returns {Promise}
   */
  static myRequest(url, params = {}, method = httpUtil.GET) {

    if (stringUtil.empty(method)) method = httpUtil.GET;

    if (method !== httpUtil.GET || method !== httpUtil.POST) method = httpUtil.GET;

    return new Promise((resolve) => {
      let temp = {
        url: url,
        method: method,
        success: function (d) {
          resolve(d.data);
        },
        error: function (e) {

          let error = {
            code: -1,
            data: null,
            msg: '服务器异常'
          };
          resolve(error);
        }
      };
      if (!stringUtil.isEmptyObject(params)) temp.data = params;
      wepy.request(temp);
    })
  }

  /**
   * 返回数据处理
   * @param state
   * @param action
   * @param successFunc  成功回调函数  (state, data)
   *        data结构: data 是一个对象，根据返回的属性，直接使用就可以了
   * @param errorFunc    失败回调函数  (state, data)
   *        data结构: {code:-1, data:null, msg:'error message'}
   *
   * @returns {{}}
   */
  static myReducers(state, action, successFunc, errorFunc) {
    let result = {...state};
    let temp = action.payload;
    if (stringUtil.isEmptyObject(temp)) {
      // 没有值，也是失败
      if (stringUtil.isExitsFunction(errorFunc)) {
        let noData = errorFunc(state, temp);
        if (stringUtil.isEmptyObject(noData)) return result;

        Object.assign(result, noData);       // 合并赋值的对象
        return result;
      }
      console.log('no data ');
      return result;
    }

    let code = temp.code;                   // 状态码
    if (code !== 0) {                       // 失败
      if (stringUtil.isExitsFunction(errorFunc)) {
        let err = errorFunc(state, temp);
        if (stringUtil.isEmptyObject(err)) return result;
        Object.assign(result, err);         // 合并赋值的对象
        return result;
      }
      console.log('失败啦啦啦');
      return result;
    }

    let data = temp.data;
    if (stringUtil.isExitsFunction(successFunc)) {
      let reducerData = successFunc(state, data);
      if (stringUtil.isEmptyObject(reducerData)) return result;
      Object.assign(result, reducerData);       // 合并赋值的对象
    }
    return result;
  }


  static loginRequest(userInfo) {

    let nickname = userInfo.nickName;
    let avatar = userInfo.avatarUrl;

    return new Promise((resolve) => {
      let error = {
        code: -1,
        data: null,
        msg: '服务器异常'
      };
      wepy.login({
        success: function (res) {
          // 这个就是openID
          let code = res.code;
          console.log(res);
          let sendData = {
            code: code,
            nickname: nickname,
            avatar: avatar
          };

          console.log(sendData);

          let temp = {
            url: LOGIN,
            data: sendData,
            method: httpUtil.GET,
            success: function (d) {
              resolve(d.data);
            },
            error: function (e) {
              resolve(error);
            }
          };
          wepy.request(temp);
        },
        error: function (error) {
          error.msg = '获取openID失败';
          resolve(error);
        }
      });
    });
  }
}

